package com.qf.sys.controller;

import com.qf.sys.po.Module;
import com.qf.sys.po.RoleAuth;
import com.qf.sys.po.User;
import com.qf.sys.service.ModuleService;
import com.qf.sys.service.RoleService;
import com.qf.sys.service.UserService;
import com.qf.sys.util.Msg;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @创建人 sunsir Email:sunsir163@163.com
 * @创建时间 2022/5/9
 * @描述
 */
@Controller
public class LoginController {
    @Resource
    UserService userService;
    @Resource
    RoleService roleService;

    @RequestMapping("login")
    @ResponseBody
    public Msg login(@Param("loginName")String loginName, @Param("password")String password,@Param("verifyCode")String verifyCode, Model model,HttpServletRequest request){

        String capText = (String)request.getSession().getAttribute("verifyCode");
        User user = userService.getUserByLoginName(loginName);
        System.out.println(password);
        System.out.println(user.getPassword());
        Msg msg = new Msg();
        System.out.println(verifyCode+"----"+capText.toLowerCase());
        if(!verifyCode.equalsIgnoreCase(capText.toLowerCase())){//验证码输入错误
            model.addAttribute("msg3","验证码输入错误！");
            msg.setCode(0);
            msg.setMsg("处理失败,您的验证码输入错误！");
            msg.setCount(200);//得到总条数
            msg.setData("error");
            return msg;
        }
        if(loginName==null||"".equals(loginName)){  //没有输入用户名
            msg.setCode(0);
            msg.setMsg("处理失败,您的用户名有误！");
            msg.setCount(200);//得到总条数
            msg.setData("error");
        }else if(user==null||"".trim().equals(user)){ //输入姓名但是姓名错误
            msg.setCode(0);
            msg.setMsg("处理失败,您的用户名有误！");
            msg.setCount(200);//得到总条数
            msg.setData("error");
        }else if(user!=null &(password==null||"".equals(password))){ //姓名正确，但没有输入密码
            msg.setCode(0);
            msg.setMsg("处理失败,请输入密码！");
            msg.setCount(200);//得到总条数
            msg.setData("error");
        }else if(user!=null &!(user.getPassword().equals(password))) { //姓名正确，输入密码，但是密码错误
            msg.setCode(0);
            msg.setMsg("处理失败,您的密码输入有误！");
            msg.setCount(200);//得到总条数
            msg.setData("error");
        }else if (user!=null &user.getPassword().equals(password)){ //姓名密码均正确
            msg.setCode(0);
            msg.setMsg("处理成功");
            msg.setCount(100);//得到总条数
            msg.setData("success");
            System.out.println(msg);
            request.getSession().setAttribute("loginUser",user);
            System.out.println(user);
            System.out.println(user.getRole());
            System.out.println(user.getRole().getId());
            List<Module> modules = roleService.getModuleByRoleId(user.getRole().getId());
            //modules.forEach(module -> System.out.println(module));
            request.getSession().setAttribute("modules",modules);
            return msg;
        }
        return msg;
    }

    @RequestMapping(value="/main")
    public String main(HttpServletRequest request){
        System.out.println("main");
        HttpSession session = request.getSession();
        if(session.getAttribute("loginUser")!= null){
            return "WEB-INF/main";
        }else {
            return "index";
        }

    }

    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }
}
